/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backoffice;

import ejb.PurchaseBeanLocal;
import ejb.UserBean;
import ejb.UserBeanLocal;
import entities.Purchase;
import entities.User;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import utils.UtilsBean;
import web.Constants;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class BackofficeUserManagedBean {

    private String pass1;
    private String pass2;
    private List<User> filteredUsers;
    private List<User> allUsers;
    private User selectedUser;
    @EJB
    private UserBeanLocal userBean;
    @EJB
    private PurchaseBeanLocal purchaseBean;

    public BackofficeUserManagedBean() {
        this.userBean = new UserBean();
    }

    public UserBeanLocal getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBeanLocal userBean) {
        this.userBean = userBean;
    }

    public List<User> selectAllUsers() {
        return this.userBean.findAll();
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;

    }

    public List<User> getFilteredUsers() {
        return filteredUsers;
    }

    public void setFilteredUsers(List<User> filteredUsers) {
        this.filteredUsers = filteredUsers;
    }

    public List<User> getAllUsers() {
        if (allUsers == null) {
            allUsers = selectAllUsers();
        }
        return allUsers;
    }

    public void setAllUsers(List<User> allUsers) {
        this.allUsers = allUsers;
    }

    public String goToDetails(User u) {
        if (u != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.BC_USER + Constants.SEPARATOR + u.getEmail());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.EXTRA1, u);
        }
        return "UPDATE";

    }

    public String getPass1() {
        return pass1;
    }

    public void setPass1(String pass1) {
        this.pass1 = pass1;
    }

    public String getPass2() {
        return pass2;
    }

    public void setPass2(String pass2) {
        this.pass2 = pass2;
    }

    public String saveProfile(User user) {
        Boolean fatalError = false;

        if (user.getEmail() == null || user.getEmail().isEmpty()) {
            fatalError = true;
        }
        if (user.getName() == null || user.getName().isEmpty()) {
            fatalError = true;
        }
        if (user.getSurname() == null || user.getSurname().isEmpty()) {
            fatalError = true;
        }
        if (user.getPhone() == null || user.getPhone().isEmpty()) {
            fatalError = true;
        }


        if (fatalError) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error no esperado"));
        } else {

            Boolean error = false;
            Boolean isNeedValidPass = pass1 != null && !pass1.isEmpty();
            if(isNeedValidPass){
            error = pass1 != null && !pass1.isEmpty() && pass2 != null && !pass2.isEmpty() && !pass1.equals("") && !pass1.equals(pass2) &&  pass1.length() > 6 && pass1.length() < 15;
            }


            if (error) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Las contraseñas deben coincidir y ser superior a 6 carácteres e inferior a 15", "Escribalas de nuevo"));
            } else {
                if (pass1 != null && !pass1.isEmpty()) {
                    user.setPass(UtilsBean.sha512(pass1));
                }

//                    user.setName(pass1);
//                    user.setSurname(pass1);
//                    user.setAddress(pass1);
//                    user.setPhone(pass1);

                userBean.addUser(user);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El usuario se ha modificado correctamente", ""));

               // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.BC_USERS + Constants.SEPARATOR + 1);

            }
        }


        return "UPDATE";
    }

    public List<Purchase> bcPurchases() {
        List<Purchase> purchases = purchaseBean.findByUser(selectedUser);
        return purchases;
    }
}
